package customerEnter.Frame.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import customerEnter.tool.WhetherToEmpty;

/**
 * 用戶安全鍋爐器
 * 
 * 类名称：LoginFilter 类描述： 创建人：dy 创建时间：2016-4-2 上午11:19:12
 * 
 */
public class LoginFilter implements Filter {

	public void destroy() {
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
			ServletException {
		HttpServletRequest request2 = (HttpServletRequest) request;
		HttpServletResponse response2 = (HttpServletResponse) response;
		String uri = request2.getRequestURI();
		String path = uri.substring(uri.lastIndexOf("/"));
		String pame = "";
		if (request2.getSession().getAttribute("CustomerValue") == null && path.contains("jsp")) {
			if (!WhetherToEmpty.StringWhetherToEmpty(request2.getQueryString())) {
				pame = "?" + request2.getQueryString();
			}
			if (seleURLIsLogin(path)) {
				request2.getSession().setAttribute("Loginforward", path + pame);
				response2.sendRedirect("Login.jsp");
			} else {
				if (!"/Login.jsp".equals(path)) {
					request2.getSession().setAttribute("LoginforwardOther", path + pame);
				}
				chain.doFilter(request, response);
			}
		} else {
			chain.doFilter(request, response);
		}
	}

	/**
	 * 判斷要登錄的jsp
	 */
	public Boolean seleURLIsLogin(String path) {
		Boolean bool = false;
		if ("/visithistory.jsp".equals(path)) {
			bool = true;
		}
		return bool;
	}

	public void init(FilterConfig filterConfig) throws ServletException {

	}

}
